package web_wms_mapper

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"transport/web/web_vo"
	"transport/web/web_wms_model"
)

type OutboundOrderDetailMapper struct {
	DB  *gorm.DB
	Ctx context.Context
}

func (mapper *OutboundOrderDetailMapper) Create(m *web_wms_model.WmOutboundOrderDetail) (ret *web_wms_model.WmOutboundOrderDetail, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Create(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *OutboundOrderDetailMapper) Update(m *web_wms_model.WmOutboundOrderDetail) (ret *web_wms_model.WmOutboundOrderDetail, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_wms_model.WmOutboundOrderDetail{}).Where("id = ?", m.ID).Updates(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *OutboundOrderDetailMapper) CreateOrUpdate(m *web_wms_model.WmOutboundOrderDetail) (ret *web_wms_model.WmOutboundOrderDetail, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(m).Save(m).Error; err != nil {
		return nil, err
	}
	return m, nil
}

func (mapper *OutboundOrderDetailMapper) QueryById(id string) (m *web_wms_model.WmOutboundOrderDetail, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_wms_model.WmOutboundOrderDetail{}).Where("id = ?", id).First(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *OutboundOrderDetailMapper) QueryByOrderCode(orderCode string) (m []*web_vo.OutboundOrderDetail, err error) {
	if orderCode == "" {
		return nil, errors.New("订单号不能为空")
	}
	if err := mapper.DB.WithContext(mapper.Ctx).Table("wm_outbound_order_detail wood").
		Select("wood.*,wpi.expression_cn,wpi.expression_en,wpi.product_code,wpi.medical_type,wpi.storage_type").
		Joins("LEFT JOIN wm_product_info wpi ON wpi.id = wood.product_id").
		Where("wood.order_code = ?", orderCode).Find(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *OutboundOrderDetailMapper) QueryByOrderCodes(orderCodes []string) (m []*web_vo.OutboundOrderDetail, err error) {
	if len(orderCodes) == 0 {
		return nil, errors.New("订单号不能为空")
	}
	if err := mapper.DB.WithContext(mapper.Ctx).Table("wm_outbound_order_detail wood").
		Select("wood.*,wpi.expression_cn,wpi.package_unit,wpi.expression_en,wpi.register_time,wpi.product_code,wpi.medical_type,wpi.storage_type"+
			",wpi.specifications,wpi.manufacturer,wpi.approval_num,wpi.permit_holder").
		Joins("LEFT JOIN wm_product_info wpi ON wpi.id = wood.product_id").
		Where("wood.order_code in (?)", orderCodes).Find(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *OutboundOrderDetailMapper) QueryByIds(ids []string) (m []*web_wms_model.WmOutboundOrderDetail, err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Model(&web_wms_model.WmOutboundOrderDetail{}).Where("id IN ?", ids).Find(&m).Error; err != nil {
		return nil, err
	}
	return m, nil
}
func (mapper *OutboundOrderDetailMapper) DeleteById(id string) (err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id = ?", id).Delete(&web_wms_model.WmOutboundOrderDetail{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *OutboundOrderDetailMapper) DeleteByIds(ids []string) (err error) {
	if err := mapper.DB.WithContext(mapper.Ctx).Unscoped().Where("id IN ?", ids).Delete(&web_wms_model.WmOutboundOrderDetail{}).Error; err != nil {
		return err
	}
	return nil
}

func (mapper *OutboundOrderDetailMapper) Page(pageIndex, pageSize int) (items []*web_wms_model.WmOutboundOrderDetail, count int64, err error) {
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx)
	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&items).Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}

func (mapper *OutboundOrderDetailMapper) List() (items []*web_wms_model.WmOutboundOrderDetail, count int64, err error) {
	var countTmp int64 = 0
	table := mapper.DB.WithContext(mapper.Ctx)
	if err := table.Find(&items).Group("id").Offset(-1).Limit(-1).Count(&countTmp).Error; err != nil {
		return nil, 0, err
	}
	return items, countTmp, nil
}
